package org.example;

import java.io.IOException;
import java.io.InputStream;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Map;

public class ReadAll {
    public void readAll() {
        String jarFilePath = "BaWangChaKun.jar";
        String targetDir = "data";
        try (FileSystem jarFileSystem = FileSystems.newFileSystem(Paths.get(jarFilePath), Map.of())) {
            Path sourceDir = jarFileSystem.getPath("data");
            Files.walkFileTree(sourceDir, new SimpleFileVisitor<>() {
                @Override
                public FileVisitResult preVisitDirectory(Path dir, BasicFileAttributes attrs) throws IOException {
                    Path targetDirPath = Paths.get(targetDir, sourceDir.relativize(dir).toString());
                    Files.createDirectories(targetDirPath);
                    return FileVisitResult.CONTINUE;
                }

                @Override
                public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
                    Path targetFilePath = Paths.get(targetDir, sourceDir.relativize(file).toString());
                    try (InputStream inputStream = Files.newInputStream(file)) {
                        Files.copy(inputStream, targetFilePath, StandardCopyOption.REPLACE_EXISTING);
                    }
                    return FileVisitResult.CONTINUE;
                }
            });
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}
